package com.ruoyi.livedemo.service;

import com.ruoyi.livedemo.dto.ProfileUpdateRequest;
import com.ruoyi.livedemo.dto.UserProfileView;
import com.ruoyi.livedemo.entity.RoutineSlot;
import com.ruoyi.livedemo.entity.User;
import com.ruoyi.livedemo.entity.UserProfile;
import com.ruoyi.livedemo.mapper.RoutineSlotMapper;
import com.ruoyi.livedemo.mapper.UserMapper;
import com.ruoyi.livedemo.mapper.UserProfileMapper;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor
public class ProfileService {

    private final UserMapper userMapper;
    private final UserProfileMapper userProfileMapper;
    private final RoutineSlotMapper routineSlotMapper;

    public UserProfileView loadProfile(Long userId) {
        User user = userMapper.findById(userId);
        if (user == null) {
            return null;
        }
        UserProfile profile = userProfileMapper.findByUserId(userId);
        List<RoutineSlot> routines = routineSlotMapper.findByUser(userId);
        return UserProfileView.builder()
                .userId(user.getId())
                .displayName(user.getDisplayName())
                .avatarPath(user.getAvatarPath())
                .email(user.getEmail())
                .themePreference(user.getThemePreference())
                .profile(profile)
                .routines(routines)
                .build();
    }

    @Transactional
    public UserProfileView upsertProfile(Long userId, ProfileUpdateRequest request) {
        User user = userMapper.findById(userId);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }

        UserProfile profile = userProfileMapper.findByUserId(userId);
        if (profile == null) {
            profile = new UserProfile();
            profile.setUserId(userId);
        }
        profile.setSelfIntroduction(request.getSelfIntroduction());
        profile.setJobTitle(request.getJobTitle());
        profile.setWorkplace(request.getWorkplace());
        profile.setHobbies(request.getHobbies());
        profile.setPartnerName(request.getPartnerName());
        profile.setPartnerTraits(request.getPartnerTraits());
        profile.setLoveStory(request.getLoveStory());
        profile.setContactWechat(request.getContactWechat());
        profile.setContactPhone(request.getContactPhone());

        if (profile.getId() == null) {
            userProfileMapper.insert(profile);
        } else {
            userProfileMapper.updateByUserId(profile);
        }

        boolean needUpdateUser = false;
        if (StringUtils.hasText(request.getDisplayName())
                && !request.getDisplayName().equals(user.getDisplayName())) {
            user.setDisplayName(request.getDisplayName());
            needUpdateUser = true;
        }
        if (request.getEmail() != null && !request.getEmail().equals(user.getEmail())) {
            user.setEmail(request.getEmail());
            needUpdateUser = true;
        }
        if (StringUtils.hasText(request.getAvatarPath())
                && !request.getAvatarPath().equals(user.getAvatarPath())) {
            user.setAvatarPath(request.getAvatarPath());
            needUpdateUser = true;
        }
        if (needUpdateUser) {
            userMapper.update(user);
        }

        return loadProfile(userId);
    }

    @Transactional
    public void updateTheme(Long userId, String theme) {
        if (!StringUtils.hasText(theme)) {
            throw new IllegalArgumentException("主题不能为空");
        }
        User user = userMapper.findById(userId);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }
        user.setThemePreference(theme);
        userMapper.update(user);
    }
}
